----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_PeacefulCityPublicQuestTracker = {}

EA_Window_PeacefulCityPublicQuestTracker.EMPTY_SIZE = { x=600, y=200 } -- Used to provide a size guide for the layout editor.

EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES = 1
EA_Window_PeacefulCityPublicQuestTracker.NUM_QUESTS = 3
EA_Window_PeacefulCityPublicQuestTracker.NUM_CONDITION_COUNTERS = 5

EA_Window_PeacefulCityPublicQuestTracker.WIDTH = 600
EA_Window_PeacefulCityPublicQuestTracker.CONDITION_NAME_WIDTH = 530

EA_Window_PeacefulCityPublicQuestTracker.currentData = {}

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

local CompleteCounterColor      = DefaultColor.TEAL
local IncompleteCounterColor    = DefaultColor.WHITE
local CompleteQuestTitleColor   = CompleteCounterColor 
local IncompleteQuestTitleColor = IncompleteCounterColor

----------------------------------------------------------------
-- Local functions
----------------------------------------------------------------
local function DisplayObjectiveOutOfRangeError(index)
    if (index)
    then
        ERROR(L"Active objective #"..index..L" updated, Peaceful City Public Quest Tracker only supports one objective")
    else
        ERROR(L"nil active objective updated, what's that mean?")
    end
end

----------------------------------------------------------------
-- EA_Window_PeacefulCityPublicQuestTracker Functions
----------------------------------------------------------------
-- OnInitialize Handler
function EA_Window_PeacefulCityPublicQuestTracker.Initialize()

    LayoutEditor.RegisterWindow( "EA_Window_PeacefulCityPublicQuestTracker",
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_PEACFUL_QUEST_TRACKER_WINDOW_NAME ),
                                GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_PEACFUL_QUEST_TRACKER_WINDOW_DESC ),
                                false, false,
                                false, nil,
                                { "topleft", "top", "topright" } )
                                

    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_ADDED,   "EA_Window_PeacefulCityPublicQuestTracker.OnQuestAdded")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_UPDATED, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestUpdated")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_REMOVED, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestRemoved")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_CONDITION_UPDATED, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestConditionUpdated")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_COMPLETED, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestCompleted")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_FAILED, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestFailed")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_RESETTING, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestResetting")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_OPTOUT, "EA_Window_PeacefulCityPublicQuestTracker.OnQuestOptOut")
            
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PUBLIC_QUEST_CHANGED_STATE,   "EA_Window_PeacefulCityPublicQuestTracker.OnQuestStateChanged")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.PLAYER_ZONE_CHANGED,          "EA_Window_PeacefulCityPublicQuestTracker.CheckToShow")
    WindowRegisterEventHandler( "EA_Window_PeacefulCityPublicQuestTracker", SystemData.Events.INTERFACE_RELOADED,           "EA_Window_PeacefulCityPublicQuestTracker.Refresh" )

    EA_Window_PeacefulCityPublicQuestTracker.UpdateFullList()
    
end

function EA_Window_PeacefulCityPublicQuestTracker.InitializeLayout()
    EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()
end

-- OnUpdate Handler
function EA_Window_PeacefulCityPublicQuestTracker.Update( timePassed )
    -- Update the timers
    EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestTimer()
end

-- OnShutdown Handler
function EA_Window_PeacefulCityPublicQuestTracker.Shutdown()

end

function EA_Window_PeacefulCityPublicQuestTracker.Refresh()
    EA_Window_PeacefulCityPublicQuestTracker.CheckToShow()
end


----------------------------------------------------------------
-- Quests
----------------------------------------------------------------
function EA_Window_PeacefulCityPublicQuestTracker.OnQuestAdded() 
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    local index = GameData.ActiveObjectives.updatedObjectiveIndex    
    
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.OnQuestAdded: index="..index )

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    -- Add the data
    EA_Window_PeacefulCityPublicQuestTracker.currentData[index] = { objectiveId=DataUtils.activeObjectivesData[index].id }
    
    -- Update the Window to Include the new objective
    EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( index )
    EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestVisibility()
    
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_ADDED )
    
end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestResetting()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.OnQuestResetting" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    local showing = WindowGetShowing( "EA_Window_PeacefulCityPublicQuestTracker" )
    
    if ( showing == false )
    then
        -- Sound
        Sound.Play( Sound.PUBLIC_QUEST_CYCLING )
    end
    
end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestUpdated() 
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.OnQuestUpdated" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    -- Update the Window to Include the new objective       
    EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( index )
    EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()
end


function EA_Window_PeacefulCityPublicQuestTracker.OnQuestRemoved()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    
    local index = GameData.ActiveObjectives.updatedObjectiveIndex
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.OnQuestRemoved() index = "..index)

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
           
    -- Slide all of the other data up on the window
    for objective = index, EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES - 1
    do
        EA_Window_PeacefulCityPublicQuestTracker.currentData[objective] = EA_Window_PeacefulCityPublicQuestTracker.currentData[objective+1]
    end
    EA_Window_PeacefulCityPublicQuestTracker.currentData[EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES] = nil
    
    -- Update the windows
    for objective = index, EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES
    do
        EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( objective )
    end
    
    EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestVisibility()
    
end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestCompleted()
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.OnQuestCompleted()")
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_COMPLETED )
end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestFailed()
    -- DEBUG(L" **EA_Window_PeacefulCityPublicQuestTracker.OnQuestFailed" )
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- Sound
    Sound.Play( Sound.PUBLIC_QUEST_FAILED )
end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestConditionUpdated()

    -- DEBUG(L" **EA_Window_PeacefulCityPublicQuestTracker.OnQuestConditionUpdated" )
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    local objective = GameData.ActiveObjectives.updatedObjectiveIndex
    local questIndex = GameData.ActiveObjectives.updatedQuestIndex
    local condition = GameData.ActiveObjectives.updatedQuestConditionIndex
    
    if( objective ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(objective)
        return -- This should never happen
    end
    
    local questData = DataUtils.activeObjectivesData[objective].Quest[questIndex]

    -- Update only the Condtion Counters
    local questWindowString = "EA_Window_PeacefulCityPublicQuestTrackerMainQuest"..questIndex
    
    for index, data in pairs(questData.conditions)
    do
    
        local conditionName = data.name
        local curCounter    = data.curCounter
        local maxCounter    = data.maxCounter
                
        local counterLabel = questWindowString.."DataCondition"..index.."Counter"
        local counterName  = questWindowString.."DataCondition"..index.."Name"

        -- Only show the conditions if conditionName is not empty
        if( conditionName ~= L"" )
        then
        -- DEBUG(L"Made it through 06 - conditions")
                  
            if( maxCounter > 0 )
            then
                LabelSetText( counterLabel, L""..curCounter..L"/"..maxCounter )

                if( curCounter == maxCounter)
                then
                    LabelSetTextColor(counterLabel, CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b )
                    LabelSetTextColor(counterName,  CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b )
                else
                    LabelSetTextColor(counterLabel, IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                    LabelSetTextColor(counterName,  IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                end         
                
                --DEBUG(L"    Condition #"..condition..L": "..curCounter..L" of "..maxCounter..L" counters.")
            else
                LabelSetText( counterLabel, L"" )
            end           
            
        end   
    end
    
end


function EA_Window_PeacefulCityPublicQuestTracker.OnQuestStateChanged()
    DataUtils.activeObjectivesData = GetActiveObjectivesData()
    -- DEBUG(L" **EA_Window_PeacefulCityPublicQuestTracker.OnQuestStateChanged" )

    local index = GameData.ActiveObjectives.updatedObjectiveIndex

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end

    -- Update the Quest Data 
    EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( index )
    EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()
end


function EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestTimer()
    
    for objectiveIndex = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES
    do
        for questIndex = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_QUESTS
        do
            local questWindowString = "EA_Window_PeacefulCityPublicQuestTrackerMainQuest"..questIndex
            
            if (DataUtils.activeObjectivesData == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex] == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest == nil) or
               (DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex] == nil)
            then
                LabelSetText( questWindowString.."TimerValue", L"" )      
                WindowSetShowing( questWindowString.."ClockImage", false )
            else
                -- Timer
                if( DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= 0 and
                    DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].maxTimer ~= nil )
                then
                    -- DEBUG(L"Made it through 05 - timer")
                    local timeLeft = DataUtils.activeObjectivesData[objectiveIndex].Quest[questIndex].timeLeft
                    local text = TimeUtils.FormatClock(timeLeft)
                    LabelSetText( questWindowString.."TimerValue", text )
                    WindowSetShowing( questWindowString.."ClockImage", true )
                else            
                    LabelSetText( questWindowString.."TimerValue", L"" )      
                    WindowSetShowing( questWindowString.."ClockImage", false )
                end
            end
        end
    end

end

-- This is only called on initialization to populate the window without any state changing animations
function EA_Window_PeacefulCityPublicQuestTracker.UpdateFullList()

    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.UpdateFullList()")
    DataUtils.activeObjectivesData = GetActiveObjectivesData()

    for objective = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES
    do

        local isObjectiveValid = false

        for questIndex = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_QUESTS
        do
            local isQuestValid = false

            if ( DataUtils.activeObjectivesData ~= nil )
            then
                if ( DataUtils.activeObjectivesData[objective] ~= nil )
                then
                    if( DataUtils.activeObjectivesData[objective].Quest[questIndex] ~= nil )
                    then
                        isQuestValid = DataUtils.activeObjectivesData[objective].Quest[questIndex].name ~= L"" and
                                       DataUtils.activeObjectivesData[objective].Quest[questIndex].name ~= nil
                    end
                end
            end
    
            local questWindowName = "EA_Window_PeacefulCityPublicQuestTrackerMainQuest"..questIndex
        
            WindowSetDimensions( questWindowName, EA_Window_PeacefulCityPublicQuestTracker.WIDTH, 0 ) 
                        
            WindowSetShowing( questWindowName, isQuestValid )
            
            isObjectiveValid = isObjectiveValid or isQuestValid
        end
        
        if( isObjectiveValid )
        then
            --DEBUG(L"Public Quest #"..objective..L" = "..GameData.ActiveObjectives[objective].name )
            --DEBUG(L"  Objective #"..objective..L" has data.")
            EA_Window_PeacefulCityPublicQuestTracker.currentData[objective] = { objectiveId=DataUtils.activeObjectivesData[objective].id }

            EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( objective )
        else
            --DEBUG(L"  Objective #"..objective..L" has no data.")
            WindowSetShowing( "EA_Window_PeacefulCityPublicQuestTracker",  false )
        end     

    end     
    
    -- I have a bug somewhere in here that's causing the window not to be sized correctly the first time through
    EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()
end

function EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestVisibility()
    if ( ( #DataUtils.activeObjectivesData == 0) or 
         (PQData.currentState ~= PQData.STATE_CLEAR) or
         (GameDefs.PeacefulCityZoneIDs[ GameData.Player.zone ] == nil) )
    then
        WindowSetShowing("EA_Window_PeacefulCityPublicQuestTracker", false)        
    else
        if (#DataUtils.activeObjectivesData ~= 1)
        then
            DisplayObjectiveOutOfRangeError(#DataUtils.activeObjectivesData)
        end

        WindowSetShowing("EA_Window_PeacefulCityPublicQuestTracker", true)
    end
    
    EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()
end

function EA_Window_PeacefulCityPublicQuestTracker.UpdateMainWindowSize()

    local height = 0
    local mainHeight = 0
    
    if WindowGetShowing("EA_Window_PeacefulCityPublicQuestTracker")
    then
    
        height = height + 10 -- 10 is the anchor offset between the top of the window and the main
        
        -- Main Window.
        for objective = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_OBJECTIVES
        do   
            -- Quests    
            for questIndex = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_QUESTS
            do
                -- Only update the window when it actually contains data
                local questWindowHeight = 0
                local questWindowName = "EA_Window_PeacefulCityPublicQuestTrackerMainQuest"..questIndex
                if( WindowGetShowing( questWindowName ) == true )
                then
                    local _, questLabelY   = LabelGetTextDimensions( questWindowName.."Label" )
                    local conditionsHeight = EA_Window_PeacefulCityPublicQuestTracker.UpdateConditionsSize( questWindowName.."Data" )
                    
                    questWindowHeight = questLabelY + conditionsHeight + 1 -- 1 is the anchor offset between the quest label and quest conditions
                                                                           -- on a EA_Window_PublicQuestTrackerQuestCluster

                    WindowSetDimensions( questWindowName.."Data", EA_Window_PeacefulCityPublicQuestTracker.WIDTH, conditionsHeight  )
                    WindowSetDimensions( questWindowName,         EA_Window_PeacefulCityPublicQuestTracker.WIDTH, questWindowHeight )
                else                        
                    WindowSetDimensions( questWindowName.."Data", EA_Window_PeacefulCityPublicQuestTracker.WIDTH, 0 ) 
                    WindowSetDimensions( questWindowName,         EA_Window_PeacefulCityPublicQuestTracker.WIDTH, 0 ) 
                end
                
                mainHeight = mainHeight + questWindowHeight
            end
            
            -- Opt out window
            local _, optOutY = WindowGetDimensions( "EA_Window_PeacefulCityPublicQuestTrackerMainOptOutButton" )
            mainHeight = mainHeight + optOutY + 8 -- 8 is the anchor offset between the quests and the opt out window
        end
        
        WindowSetDimensions( "EA_Window_PeacefulCityPublicQuestTrackerMain", EA_Window_PeacefulCityPublicQuestTracker.WIDTH, mainHeight )
    end
    
    height = height + mainHeight
    WindowSetDimensions( "EA_Window_PeacefulCityPublicQuestTracker", EA_Window_PeacefulCityPublicQuestTracker.WIDTH, height )
    
end

function EA_Window_PeacefulCityPublicQuestTracker.UpdateConditionsSize( dataWindow )
    -- Resize on Conditions
    local conditionHeight = 0
    for condition = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_CONDITION_COUNTERS
    do
        local windowName = dataWindow.."Condition"..condition
        local nameWindowName = windowName.."Name"
        if( WindowGetShowing(windowName) == true )
        then
            local _, conditionTextHeight = LabelGetTextDimensions( nameWindowName )
            WindowSetDimensions( nameWindowName, EA_Window_PeacefulCityPublicQuestTracker.CONDITION_NAME_WIDTH, conditionTextHeight ) 
            WindowSetDimensions( windowName,     EA_Window_PeacefulCityPublicQuestTracker.WIDTH,                conditionTextHeight ) 
            conditionHeight = conditionHeight + conditionTextHeight
        else
            WindowSetDimensions( nameWindowName, EA_Window_PeacefulCityPublicQuestTracker.CONDITION_NAME_WIDTH, 0 ) 
            WindowSetDimensions( windowName,     EA_Window_PeacefulCityPublicQuestTracker.WIDTH,                0 ) 
        end
    end
    
    return conditionHeight
end

-- Updates the Quest & Condition Data for an Objective
function EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker( objective )     
   
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.UpdateTracker("..objective..L"): #"..GameData.ActiveObjectives[objective].id..L" '"..GameData.ActiveObjectives[objective].name..L"' control = "..GameData.ActiveObjectives[objective].curControlPoints )
    if (objective ~= 1)
    then
        DisplayObjectiveOutOfRangeError(objective)
        return
    end
    
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local objectiveWindowString = "EA_Window_PeacefulCityPublicQuestTrackerMain"

    -- If this index isn't used or it is a battlefield objective, hide the objective tracker
    local showObjective = objectiveData ~= nil and not objectiveData.isBattlefieldObjective
    WindowSetShowing( objectiveWindowString, showObjective )
    
    -- nothing else to do
    if ( objectiveData == nil ) or
       ( objectiveData.isBattlefieldObjective )
    then
        return
    end    

     -- set opt out flag
    ButtonSetPressedFlag( objectiveWindowString.."OptOutButton", objectiveData.optedOutForLoot )
    
    local displayData = EA_Window_PeacefulCityPublicQuestTracker.currentData[objective]
    
    -- Name and Difficulty    
    if( objectiveData.difficulty == GameData.PublicQuestDifficulty.HARD )
    then
        LabelSetTextColor( "EA_Window_PeacefulCityPublicQuestTrackerMainQuest1Label", DefaultColor.RED.r, DefaultColor.RED.g, DefaultColor.RED.b )
    else
        LabelSetTextColor( "EA_Window_PeacefulCityPublicQuestTrackerMainQuest1Label", DefaultColor.YELLOW.r, DefaultColor.YELLOW.g, DefaultColor.YELLOW.b )
    end    
    
    -- Quests
    for questIndex = 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_QUESTS
    do
        -- DEBUG(L"Made it through 02")
        local questData = objectiveData.Quest[questIndex]
        local questWindowString = "EA_Window_PeacefulCityPublicQuestTrackerMainQuest"..questIndex
        
        if ( questData ~= nil )
        then
            -- DEBUG(L"Made it through 03")    
            
            WindowSetShowing( questWindowString, true )
            --DEBUG(L"  Quest #"..questIndex..L" '"..GameData.ActiveObjectives[objective].Quest[questIndex].name..L"'")
            
            -- Execute special layout for quests past the first one if the realm is the same as the
            --   previous quest's realm.
            -- TODO: Fix this so that the server can send the list of quests with various realms' quests interleaved
            local realmIsSameAsPrevious = false
            if( questIndex > 1 )
            then 
                realmIsSameAsPrevious = (questData.availableRealm == 0) or
                                        (questData.availableRealm == 
                                        DataUtils.activeObjectivesData[objective].Quest[questIndex - 1].availableRealm)
            end
            
            local useOrForName = realmIsSameAsPrevious

            -- Name
            local name = L""
            if (useOrForName)
            then
                name = GetString(StringTables.Default.LABEL_ALTERNATE_QUEST_CHOICE_PREFIX )
            elseif (questData.availableRealm ~= GameData.Player.realm)
            then
                if (questData.availableRealm == GameData.Realm.ORDER)
                then
                    name = GetString(StringTables.Default.LABEL_ORDER)
                elseif (questData.availableRealm == GameData.Realm.DESTRUCTION)
                then
                    name = GetString(StringTables.Default.LABEL_DESTRUCTION)
                else
                    name = DataUtils.activeObjectivesData[objective].name
                end
                name = name..L" - "..DataUtils.activeObjectivesData[objective].Quest[questIndex].name
            else
                name = DataUtils.activeObjectivesData[objective].name
                name = name..L" - "..DataUtils.activeObjectivesData[objective].Quest[questIndex].name
            end
            -- DEBUG(L"Made it through 04")
            LabelSetText(questWindowString.."Label", name )
            --DEBUG(L"  Quest #"..questIndex..L" '"..name..L"' with dimensions: x="..x..L", y="..y )

            EA_Window_PeacefulCityPublicQuestTracker.UpdateQuestTimer()          
          
            -- Conditions
            for index, data in ipairs(questData.conditions)
            do
                local conditionName = data.name
                local curCounter    = data.curCounter
                local maxCounter    = data.maxCounter
                local isFailureCondition = data.failureCondition
                
                local nameLabel    = questWindowString.."DataCondition"..index.."Name"
                local counterLabel = questWindowString.."DataCondition"..index.."Counter"
                local typeImage    = questWindowString.."DataCondition"..index.."Type"

                -- Only show the conditions if conditionName is not empty
                if( conditionName ~= L"" )
                then
                -- DEBUG(L"Made it through 06 - conditions")
                    
                    if( maxCounter > 0 )
                    then
                        LabelSetText( nameLabel, conditionName..L" - " )
                        LabelSetText( counterLabel, L""..curCounter..L"/"..maxCounter )

                        if( curCounter == maxCounter)
                        then
                            LabelSetTextColor(counterLabel, CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b )
                            LabelSetTextColor(nameLabel,    CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b )
                        else
                            LabelSetTextColor(counterLabel, IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                            LabelSetTextColor(nameLabel,    IncompleteCounterColor.r, IncompleteCounterColor.g, IncompleteCounterColor.b)
                        end         
                        
                        --DEBUG(L"    Condition #"..condition..L": "..curCounter..L" of "..maxCounter..L" counters.")
                    else
                        LabelSetText( nameLabel, conditionName )            
                        LabelSetText( counterLabel, L"" )
                    end
                    
                    if (isFailureCondition)
                    then
                        DynamicImageSetTextureSlice(typeImage, "PQ-Failure-Condition")
                    else
                        DynamicImageSetTextureSlice(typeImage, "PQ-Success-Condition")
                    end
                             
                    WindowSetShowing(questWindowString.."DataCondition"..index, true )
                else
                    LabelSetText( nameLabel, L"" )
                    LabelSetText( counterLabel, L"" )
                    WindowSetShowing(questWindowString.."DataCondition"..index, false)
                end
                
            end
            
            for index = #questData.conditions + 1, EA_Window_PeacefulCityPublicQuestTracker.NUM_CONDITION_COUNTERS
            do
                local nameLabel    = questWindowString.."DataCondition"..index.."Name"
                local counterLabel = questWindowString.."DataCondition"..index.."Counter"

                LabelSetText( nameLabel, L"" )
                LabelSetText( counterLabel, L"" )
                WindowSetShowing(questWindowString.."DataCondition"..index, false)
            end
                       
        else
            WindowSetShowing(questWindowString, false)
        end
        
    end        
    -- DEBUG(L"Made it through 07 - exiting")

end


function EA_Window_PeacefulCityPublicQuestTracker.MouseOverQuest()

    local objective  = 1
    local questIndex = WindowGetId(SystemData.ActiveWindow.name)
    -- DEBUG(L"EA_Window_PeacefulCityPublicQuestTracker.MouseOverQuest - ActiveObjective Index = "..objective..L", quest index = "..questIndex )

    if (DataUtils.activeObjectivesData[objective] == nil)
    then
        return
    elseif ( DataUtils.activeObjectivesData[objective].Quest[questIndex] == nil )
    then
        return
    end
    
    local objectiveData = DataUtils.activeObjectivesData[objective]
    local questData     = objectiveData.Quest[questIndex]

    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )    
    local row = 1
    local column = 1

    -- Name
    local text = questData.name
    Tooltips.SetTooltipFont( row, column, "font_default_sub_heading", WindowUtils.FONT_DEFAULT_SUB_HEADING_LINESPACING  )
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, 255, 204, 102 )
    
    -- Complete Label
    column = column + 1 
    if( questData.complete == true )
    then
        text = GetString( StringTables.Default.LABEL_COMPLETE )         
        Tooltips.SetTooltipColor( row, column, CompleteCounterColor.r, CompleteCounterColor.g, CompleteCounterColor.b)
        Tooltips.SetTooltipText( row, column, text )
    end
    row = row + 1
    column = 1
    
    -- Objective Label
    text = GetString( StringTables.Default.LABEL_PUBLIC_QUEST ) 
    -- Difficulty
    local difficultyText = TrackerUtils.GetDifficultyText( objectiveData.difficulty )
    text = text..L" ("..difficultyText..L")"
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, 100, 100, 100)
    
    row = row + 1
    
    -- Text
    local text = questData.desc
    Tooltips.SetTooltipText( row, column, text )
    row = row + 1
    column = 1
    
    -- Difficulty help
    text = TrackerUtils.GetDifficultyHelpText( objectiveData.difficulty )
    local color = TrackerUtils.GetDifficultyColor( objectiveData.difficulty )
    Tooltips.SetTooltipText( row, column, text )
    Tooltips.SetTooltipColor( row, column, color.r, color.g, color.b )

    Tooltips.Finalize()
    Tooltips.AnchorTooltip( Tooltips.ANCHOR_WINDOW_LEFT )   
end

----------------------------------------------------------------
-- Window manipulation
----------------------------------------------------------------

function EA_Window_PeacefulCityPublicQuestTracker.CheckToShow()
    
    local showPeacefulPublicQuestTracker = false    
    
    if (GameDefs.PeacefulCityZoneIDs[ GameData.Player.zone ] ~= nil)
    then
        WindowSetShowing("EA_Window_PeacefulCityPublicQuestTracker", true)        
        EA_Window_PeacefulCityPublicQuestTracker.UpdateFullList()
    else
        WindowSetShowing("EA_Window_PeacefulCityPublicQuestTracker", false)
    end
end


function EA_Window_PeacefulCityPublicQuestTracker.ToggleOptOut()
     
    local optedOutForLootRoll = ButtonGetPressedFlag( "EA_Window_PeacefulCityPublicQuestTrackerMainOptOutButton" )   
    TrackerUtils.OnMouseOverOptOut()
    
    local objective = 1    
    if( DataUtils.activeObjectivesData[objective] )
    then
        local objectiveId = DataUtils.activeObjectivesData[objective].id
        LootRollOptOut(objectiveId, optedOutForLootRoll) 
    end

end

function EA_Window_PeacefulCityPublicQuestTracker.OnQuestOptOut(index, optOut)

    if( index ~= 1 )
    then
        DisplayObjectiveOutOfRangeError(index)
        return -- This should never happen
    end
    
    ButtonSetPressedFlag( "EA_Window_PeacefulCityPublicQuestTrackerMainOptOutButton", optOut )

    DataUtils.activeObjectivesData[index].optedOutForLoot = optOut
end
